package com.accompany.fragment.my.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.accompany.R;
import com.accompany.adapter.BaseRecyclerHolder;
import com.accompany.adapter.BaseRecyclerRefreshAdapter;
import com.accompany.api.bean.ExtractMoneyRecordBean;
import com.accompany.api.category.ExtractMoneyRecordCategory;
import com.accompany.api.presenter.ExtractMoneyRecordPresenter;
import com.accompany.base.BaseActivity;
import com.accompany.main.ConstantStaticUrl;
import com.accompany.utils.FullScreenUtils;
import com.accompany.utils.SPUtils;
import com.accompany.utils.ToastUtil;
import com.accompany.view.RecyclerViewLayoutManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 信息说明: 提现记录
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/4/27 22 ：36
 * 项目名称：yuanban
 */
public class ExtractMoneyRecordActivity extends BaseActivity implements ExtractMoneyRecordCategory.View, OnLoadMoreListener, OnRefreshListener {
    @BindView(R.id.base_title)
    TextView mTitleMesg;
    @BindView(R.id.base_refresh_recycler)
    RecyclerView mRecyclerView;
    @BindView(R.id.relat_empty)
    RelativeLayout mEmptyLayout;
    @BindView(R.id.base_refresh)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.icn_image)
    ImageView icnImage;
    private ExtractMoneyRecordPresenter mPreseneter;
    private String mToken;
    private long mUserId;
    private List<ExtractMoneyRecordBean.ExtractMoneyRecordBeanData.ExtractMoneyRecordBeanDataList> mlist = new ArrayList<>();
    private BaseRecyclerRefreshAdapter<ExtractMoneyRecordBean.ExtractMoneyRecordBeanData.ExtractMoneyRecordBeanDataList> mAdapter;
    private int pageNumber = 1;
    private boolean isLoading = false;
    private ExtractMoneyRecordBean baseBean;

    @Override
    public int getLayoutId() {
        return R.layout.activity_extract_money_record;
    }

    @Override
    public void initLayoutView() {
        FullScreenUtils.getInstance().fullScreen(ExtractMoneyRecordActivity.this, true);
        mTitleMesg.setText("提现记录");
        RecyclerViewLayoutManager.setLinearVerticalManager(ExtractMoneyRecordActivity.this, mRecyclerView);
        initRecyclerView();
        mAdapter = new BaseRecyclerRefreshAdapter<ExtractMoneyRecordBean.ExtractMoneyRecordBeanData.ExtractMoneyRecordBeanDataList>(ExtractMoneyRecordActivity.this, mlist, R.layout.record_money_item) {
            @Override
            public void convert(BaseRecyclerHolder holder, ExtractMoneyRecordBean.ExtractMoneyRecordBeanData.ExtractMoneyRecordBeanDataList item, int position, boolean isScrolling) throws IOException {
                holder.setText(R.id.order_number, String.valueOf(item.getId()));
                holder.setText(R.id.order_price, String.valueOf("￥" + item.getExtractPrice()));
                holder.setText(R.id.aount_price, String.valueOf(item.getBankCode()));
                holder.setText(R.id.fukun_time, item.getCreateTime());
                LinearLayout ll = holder.getView(R.id.ll_reason);
                TextView textView = holder.getView(R.id.tv_reason);
                if (item.getStatus() == -1){ // -1 未通过 0 审核中 1 已提现
                    holder.setImageResource(R.id.img_flag,R.drawable.audit_failure_bg);
                    if(!TextUtils.isEmpty(item.getMark())){
                        ll.setVisibility(View.VISIBLE);
                        textView.setVisibility(View.VISIBLE);
                        textView.setText(item.getMark());
                    }
                }else if(item.getStatus() == 0){
                    holder.setImageResource(R.id.img_flag,R.drawable.ver_withdraw_bg);
                    ll.setVisibility(View.GONE);
                    textView.setVisibility(View.GONE);
                }else if ( item.getStatus() == 1){
                    holder.setImageResource(R.id.img_flag,R.drawable.inaccount_bg);
                    ll.setVisibility(View.GONE);
                    textView.setVisibility(View.GONE);
                }
            }
        };
        mAdapter.setDatas(mlist);
        mRecyclerView.setAdapter(mAdapter);
        mPreseneter = new ExtractMoneyRecordPresenter(this);
        SetPersenter(mPreseneter);
        mToken = (String) SPUtils.get(ExtractMoneyRecordActivity.this, ConstantStaticUrl.mToken, "");
        SharedPreferences sharedPreferences = ExtractMoneyRecordActivity.this.getSharedPreferences("First", MODE_PRIVATE);
        mUserId = sharedPreferences.getLong(ConstantStaticUrl.userId, 0);
        mPreseneter.getExtractMoneyRecordlist(mToken, mUserId, pageNumber);
        smartRefreshLayout.setOnLoadMoreListener(this);
        smartRefreshLayout.setOnRefreshListener(this);

    }

    private void initRecyclerView() {
    }

    @OnClick({R.id.base_back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.base_back:
                finishWitchAnimation();
                break;
        }

    }

    @Override
    public void getExtractMoneyRecordlist(ExtractMoneyRecordBean baseBean) {
        this.baseBean = baseBean;
        if (baseBean.getCode() == ConstantStaticUrl.SUCCESS_CODE) {
            if (baseBean.getData() != null && baseBean.getData().getList().size() > 0) {
                if (pageNumber == 1) {//第一次请求
                    if (isLoading) {
                        isLoading = false;
                    } else {
                        mlist.clear();
                    }
                    mlist.addAll(baseBean.getData().getList());
                    mAdapter.setDatas(baseBean.getData().getList());
                    smartRefreshLayout.setEnableLoadMore(true);
                } else {
                    if (baseBean.getData().isHasNextPage()) {
                        mlist.addAll(mlist.size(), baseBean.getData().getList());
                        mAdapter.add(baseBean.getData().getList());
                        smartRefreshLayout.setEnableLoadMore(true);
                    } else {
                        smartRefreshLayout.setEnableLoadMore(false);
                        ToastUtil.showToastINFO(ExtractMoneyRecordActivity.this, "没有更多数据");
                    }
                }
                mEmptyLayout.setVisibility(View.GONE);
                mRecyclerView.setVisibility(View.VISIBLE);
            } else {
                if (isLoading) {
                    isLoading = false;
                } else {
                    mEmptyLayout.setVisibility(View.VISIBLE);
                    mRecyclerView.setVisibility(View.GONE);
                }
            }
        } else {
            ToastUtil.showToastINFO(ExtractMoneyRecordActivity.this, baseBean.getMsg());
        }
    }

    @Override
    public void SetPersenter(Object persenter) {

    }

    @Override
    public void OnShowErrorMsg(String mErrorMsg) {
        ToastUtil.showToastERROR(ExtractMoneyRecordActivity.this, mErrorMsg);

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        if ( baseBean.getData().isHasNextPage()){
            isLoading = true;
            pageNumber++;
            mPreseneter.getExtractMoneyRecordlist(mToken, mUserId, pageNumber);
        }else {
            isLoading = false;
        }
        smartRefreshLayout.finishLoadMore(500);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNumber = 1;
        mPreseneter.getExtractMoneyRecordlist(mToken, mUserId, pageNumber);
        smartRefreshLayout.finishRefresh(500);
    }

}
